package com.jgy.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jgy.client.FileClient;
import com.jgy.entity.Goods;
import com.jgy.entity.vo.GoodsVo;
import com.jgy.entity.vo.GoodsWordVo;
import com.jgy.service.GoodsService;
import com.jgy.mapper.GoodsMapper;
import com.jgy.utils.MapperUtils;
import com.jgy.utils.R;
import com.jgy.utils.ResponseHeader;
import com.jgy.utils.ZipUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @author light
* @description 针对表【goods】的数据库操作Service实现
* @createDate 2023-04-01 16:37:37
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
    implements GoodsService{

    @Autowired
    private GoodsMapper goodsMapper;
    @Resource
    private FileClient fileClient;
    /**
     * 商品导出
     * @param goodsVo 导出参数条件
     * @param response 响应信息
     */
    @Override
    public void exportGoods(GoodsVo goodsVo, HttpServletResponse response) {
        ResponseHeader responseHeader = new ResponseHeader();
        String fileName = "商品表.xlsx";
        QueryWrapper<Goods> query = new QueryWrapper<>();
        if (StrUtil.isNotBlank(goodsVo.getSearch())) {
            query.lambda().like(Goods::getGoods, goodsVo.getSearch());
        }
        List<Goods> goodsList = goodsMapper.selectList(query);
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("sheet1");
        XSSFRow row = sheet.createRow(0);
        //表头
        for (int i = 0; i < 13; i++) {
            row.createCell(i).setCellValue(i);
        }
        for (int i = 1; i < goodsList.size(); i++) {
            XSSFRow row1 = sheet.createRow(i);
            Goods goods = goodsList.get(i - 1);
                row1.createCell(1).setCellValue(goods.getGoods());
            row1.createCell(2).setCellValue(goods.getPrice().toString());
            row1.createCell(3).setCellValue(goods.getIntroductions());
            row1.createCell(4).setCellValue(goods.getSecond());
            row1.createCell(5).setCellValue(goods.getThird());

        }
        //响应到客户端
        try {
            responseHeader.setResponseHeader(response, fileName);
            OutputStream out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用service服务的上传接口
     * @param file 上传文件
     * @return 下载url
     */
    @Override
    public R uploadFile(MultipartFile file) {
        return fileClient.uploadFile(file);
    }

    /**
     * 商品导出word
     * 引用word模板，填充商品数量，价格等信息
     * @param response 响应信息
     */
    @Override
    public void wordGoods(HttpServletResponse response) throws FileNotFoundException {
        /**
         * 模板路径
         */
        String templatePath = "E:/springcloud/space-pro/space-order/src/main/resources/template/goods.docx";

        /**
         * 压缩zip路径
         */
        String templateZip = "E:/word/poi/goods.zip";
        //生成的word路径
        String fileDir = "E:/word/";
        //生成的文件名字
        String fileName = "goodscount";
        List<String> filePath = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>(16);
        List<Goods> goodsList = goodsMapper.selectList(null);
        //商品总金额
        double sum = goodsList.stream().mapToDouble(data -> Double.parseDouble(data.getPrice().toString())).sum();
        //商品总数量
        int total = goodsList.size();
        map.put("sum",sum);
        map.put("total",total);

        //通过模板创建word文档
        ZipUtils.createWord(templatePath,fileDir,fileName,map,null);
        filePath.add(fileDir+fileName+".docx");

        //将创建的word文档压缩成zip
        ZipUtils.createZip(templateZip, filePath);

        //下载zip
        ZipUtils.downLoadFile(templateZip,response);

        ZipUtils.delAllFile(fileDir);
    }
}




